import type { itemState } from '~/../typings/types/city'
import { doAdd, doEdit } from '~/api/destination'
interface queryFormState {
  roadLineCustomerId?: string
  destProvince?: string
  destCity?: string
  destArea?: string
  number?: number
  type: 1 | 2
  typeSort?: number | string
  contact?: string
  telephone?: string
  address?: string
}
function toast(title: string) {
  wx.showToast({
    icon: 'none',
    title
  })
}
// pages/tabbar/order/components/FormPop.ts
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    visible: false as boolean,
    queryForm: {
      roadLineCustomerId: '',
      destProvince: '',
      destCity: '',
      destArea: '',
      number: 1,
      type: 1,
      typeSort: '',
      contact: '',
      telephone: '',
      address: ''
    } as queryFormState
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getValue(this: any) {
      return this.data.queryForm
    },
    async confirm(this: any) {
      const { queryForm } = this.data
      if (!queryForm.destProvince) {
        toast('请选择地区')
        return
      }
      if (!queryForm.address) {
        toast('请输入联系地址')
        return
      }
      if (!queryForm.contact) {
        toast('请输入联系人员')
        return
      }
      if (!queryForm.telephone) {
        toast('请输入联系电话')
        return
      }
      if (!queryForm.telephone) {
        toast('请输入联系电话')
        return
      }
      if (!queryForm.number) {
        toast('请输入线路')
        return
      }
      if (queryForm.id) {
        await doEdit(queryForm)
      } else {
        await doAdd(queryForm)
      }
      this.triggerEvent("success", queryForm)
      this.close()
    },
    typeChange(this: any, e: any) {
      const dataset = e.currentTarget.dataset
      const { queryForm } = this.data
      queryForm.type = dataset.name
      this.setData({
        queryForm
      })
    },
    handleChange(this: any, e: any) {
      const { queryForm } = this.data
      queryForm.number = e.detail.value
      this.setData({
        queryForm
      })
    },
    // input 双向绑定
    bindKeyInput(this: any, e: any) {
      const dataset = e.currentTarget.dataset
      const { queryForm } = this.data
      queryForm[dataset.params] = e.detail.value
      if (this.data.queryForm) {
        this.setData({
          queryForm
        })
      }
    },
    handJoin(this: any) {
      wx.navigateTo({
        url: '/pages/city/index?type=destination',
        events: {
          chooseEvent: (e: itemState[]) => {
            const { queryForm } = this.data
            if (e.length === 1) {
              queryForm.destProvince = e[0].label
              queryForm.destCity = ''
              queryForm.destArea = ''
            } else if (e.length === 2) {
              queryForm.destProvince = e[0].label
              queryForm.destCity = e[1].label
              queryForm.destArea = ''
            } else if (e.length === 3) {
              queryForm.destProvince = e[0].label
              queryForm.destCity = e[1].label
              queryForm.destArea = e[2].label
            }
            this.setData({
              queryForm
            })
          }
        }
      })
    },
    open(this: any, form?: any) {
      const params: {
        visible: boolean,
        queryForm?: any
      } = {
        visible: true
      }
      if (form?.id) {
        params.queryForm = form
      } else {
        params.queryForm = {
          roadLineCustomerId: form.roadLineCustomerId,
          destProvince: '',
          destCity: '',
          destArea: '',
          number: 1,
          type: 1,
          typeSort: '',
          contact: '',
          telephone: '',
          address: ''
        }
      }
      this.setData(params);
    },
    close(this: any) {
      this.setData({
        visible: false
      })
    },
    onVisibleChange(this: any, e: any) {
      if (!e.detail.visible) {
        this.close()
      }
    },
  }
})